package com.woniuxy.threeproject.end_water_project_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.threeproject.end_water_project_user.domain.Meterreading;
import com.woniuxy.threeproject.end_water_project_user.dao.MeterreadingMapper;
import com.woniuxy.threeproject.end_water_project_user.domain.Watermeter;
import com.woniuxy.threeproject.end_water_project_user.service.IMeterreadingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 水量表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-25
 */
@Service
@Slf4j
public class MeterreadingServiceImpl extends ServiceImpl<MeterreadingMapper, Meterreading> implements IMeterreadingService {
    @Resource
    public MeterreadingMapper meterreadingMapper;
    @Override
    public List<Meterreading> getWaterNum(Integer waterMeterId) {
        QueryWrapper<Meterreading> qw = new QueryWrapper<>();
        qw.eq("water_meter_id",waterMeterId);
        qw.eq("state",0);
        qw.orderByAsc("meter_reading_time");
        List<Meterreading> metters = meterreadingMapper.selectList(qw);
        return metters;
    }

    @Override
    public int updateStaet(Integer waterMeterId) {
//        先按水表id查询几个月
        QueryWrapper<Meterreading> qw1 = new QueryWrapper<>();
        qw1.eq("water_meter_id",waterMeterId);
        List<Meterreading> ms = meterreadingMapper.selectList(qw1);
        boolean bool=false;
        for (int i = 0; i < ms.size(); i++) {
            Integer id = ms.get(i).getMeterReadingId();
//            ms.get(i).setState(1);
        //按照每个月的水量id修改其状态
            UpdateWrapper<Meterreading> qw2 = new UpdateWrapper<>();
            qw2.set("state",1);
            qw2.eq("meter_reading_id",id);
            int ri = meterreadingMapper.update(null,qw2);
            bool=true;
        }
        if(bool){
            log.info("修改状态成功！");
            return 1;
        }
        return 0;
    }
}
